package Offer2019;

import java.util.List;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.TimeUnit;

/**
 * 阻塞队列
 */
public class BlockingQueueDemo {

    //抛出异常
    //public static void main(String[] args) {
    //    BlockingQueue<String> blockingQueue = new ArrayBlockingQueue<>(3);
    //
    //    System.out.println(blockingQueue.add("a"));
    //    System.out.println(blockingQueue.add("b"));
    //    System.out.println(blockingQueue.add("c"));
    //    //java.lang.IllegalStateException: Queue full
    //    //System.out.println(blockingQueue.add("d"));
    //    //返回当前队列头元素
    //    System.out.println(blockingQueue.element());
    //
    //    System.out.println(blockingQueue.remove());
    //    System.out.println(blockingQueue.remove());
    //    System.out.println(blockingQueue.remove());
    //    //java.util.NoSuchElementException
    //    //System.out.println(blockingQueue.remove());
    //    //java.util.NoSuchElementException
    //    System.out.println(blockingQueue.element());
    //}

    //返回特殊值
    //public static void main(String[] args) {
    //
    //    BlockingQueue<String> blockingQueue = new ArrayBlockingQueue<>(3);
    //
    //    System.out.println(blockingQueue.offer("a"));
    //    System.out.println(blockingQueue.offer("b"));
    //    System.out.println(blockingQueue.offer("c"));
    //    //队列满
    //    System.out.println(blockingQueue.offer("d"));//false
    //    //查看当前队列头元素
    //    System.out.println(blockingQueue.peek());
    //    System.out.println(blockingQueue.poll());
    //    System.out.println(blockingQueue.poll());
    //    System.out.println(blockingQueue.poll());
    //    //队列空
    //    System.out.println(blockingQueue.poll());//null
    //    System.out.println(blockingQueue.peek());//null
    //}

    //一直阻塞
    //public static void main(String[] args) throws InterruptedException {
    //
    //    BlockingQueue<String> blockingQueue = new ArrayBlockingQueue<>(3);
    //
    //    blockingQueue.put("a");
    //    blockingQueue.put("b");
    //    blockingQueue.put("c");
    //    System.out.println("全部添加完成！");
    //    //队列满
    //    //blockingQueue.put("d");//false
    //
    //    System.out.println(blockingQueue.take());
    //    System.out.println(blockingQueue.take());
    //    System.out.println(blockingQueue.take());
    //    //队列为空
    //    System.out.println(blockingQueue.take());
    //}

    //超时退出
    public static void main(String[] args) throws InterruptedException {

        BlockingQueue<String> blockingQueue = new ArrayBlockingQueue<>(3);

        blockingQueue.put("a");
        blockingQueue.put("b");
        blockingQueue.put("c");
        System.out.println("全部添加完成！");
        //队列满 等待一秒
        System.out.println(blockingQueue.offer("d", 1l, TimeUnit.SECONDS));

        System.out.println(blockingQueue.take());
        System.out.println(blockingQueue.take());
        System.out.println(blockingQueue.take());
        //队列为空 等待一秒
        System.out.println(blockingQueue.poll(1l, TimeUnit.SECONDS));
    }

}

